import {
  listComFinancialYear,
  getComFinancialYear,
  delComFinancialYear,
  addComFinancialYear,
  updateComFinancialYear,
} from "@/apis/user/financialYear";
import { getCookiesUserInfo } from "@/utils/auth";

export const api = {
  list: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await listComFinancialYear(queryParams);
    return res;
  },
  one: getComFinancialYear,
  delete: delComFinancialYear,
  add: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addComFinancialYear(queryParams);
    return res;
  },
  update: updateComFinancialYear,
  download: (queryParams) => {
    this.download(
      "company/ComFinancialYear/export",
      {
        ...queryParams,
      },
      `ComFinancialYear_${new Date().getTime()}.xlsx`
    );
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 50,
    fixed: true,
  },
  {
    title: "年度",
    dataIndex: "year",
    dataType: "Date",
    inQuery: true,
    inTable: true,
    inForm: true,
    width: 200,
    fixed: true,
  },
  {
    title: "营业额",
    dataIndex: "turnover",
    dataType: "Number",
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 200,
    unit: "万元",
  },
  {
    title: "总支出",
    dataIndex: "totalSpend",
    dataType: "Number",
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 200,
    unit: "万元",
  },
  {
    title: "净利润",
    dataIndex: "netProfit",
    dataType: "Number",
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 200,
    unit: "万元",
  },
  {
    title: "缴税金额",
    dataIndex: "taxPaid",
    dataType: "Number",
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 200,
    unit: "万元",
  },
  {
    title: "描述",
    dataIndex: "description",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
    width: 200,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
    width: 200,
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Range-picker";
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item, index) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
      width: item.width,
    };
    if (item.fixed) {
      newItem.fixed = item.fixed;
    }
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        let option = item.options.find((option) => {
          return option.value == record[item.dataIndex];
        });
        return option ? option.label : "-";
      };
    }
    if (item.unit) {
      newItem.title = newItem.title + "(" + item.unit + ")";
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    scopedSlots: { customRender: "action" },
    width: 180,
    fixed: "right",
  });
  console.log("items", items);

  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
      newItem.title = newItem.title + "（" + item.unit + "）";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Date-picker";
    } else if (item.dataType == "File") {
      newItem.component = "Upload";
    }
    return newItem;
  });
  return items;
};

export const rules = {
  financialDate: [{ required: true, message: "必须输入交易日期" }],
  account: [{ required: true, message: "必须输入账户" }],
  debit: [{ required: true, message: "必须输入借方金额" }],
  credit: [{ required: true, message: "必须输入贷方金额" }],
  transactionType: [{ required: true, message: "必须选择交易类型" }],
  counterpartyAccount: [{ required: true, message: "必须输入对方账户" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 6 },
    wrapperCol: { span: 14 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
